package operation;

import book.BookList;
import book.Book;

import java.math.BigDecimal;
import java.util.Scanner;

public class UpdateOperation extends DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要修改的书名: ");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();
        int index = search(bookList, bookName);
        if (index != -1) {
            Book book = bookList.getBook(index);
            removeBook(bookList,index);
            System.out.println("请按要求输入修改的结果,如不修改无需输入内容: ");
            while(true) {
                System.out.println("输入修改后书名: ");
                bookName = scanner.nextLine();
                int idx = search(bookList,bookName);
                if(!bookName.isEmpty()) {
                    if(idx == -1) {
                        book.setName(bookName);
                        break;
                    } else {
                        System.out.println("当前已经有书名为《"+bookName+"》的图书,请重新输入!");
                    }
                }
            }
            System.out.println("输入修改后作者: ");
            String author = scanner.nextLine();
            if(!author.isEmpty()) {
                book.setAuthor(author);
            }
            System.out.println("输入修改后价格: ");
            String priceStr = scanner.nextLine();
            if(!priceStr.isEmpty()) {
                BigDecimal price = new BigDecimal(priceStr);
                book.setPrice(price);
            }
            System.out.println("输入修改后类别: ");
            String type = scanner.nextLine();
            if(!type.isEmpty()) {
                book.setType(type);
            }
            new AddOperation().add(bookList,book);
        } else {
            System.out.println("未查找到图书《"+bookName+"》!");
        }
    }
}
